package com.tsu.mall.order.service.impl;

import com.tsu.mall.common.VO.CartWaresVO;
import com.tsu.mall.common.entity.Member;
import com.tsu.mall.order.entity.Cart;
import com.tsu.mall.order.exception.BusinessException;
import com.tsu.mall.order.exception.NoLoginException;
import com.tsu.mall.order.mapper.CartMapper;
import com.tsu.mall.order.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsu.mall.order.util.OrderSessionUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tttttt
 * @since 2023-04-05
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    //加入购物车操作
    @Override
    public void addCart(Integer waresId, Integer waresNum,String memberWaresColor,String memberWaresVersion) {
        Member m= OrderSessionUtil.getCurrent();
        Cart cart=new Cart();
        //没登录
        if(m==null){
            //跳转登录页面
            throw new NoLoginException();
        }
        //已经登录过了
        //判断这些产品是否之前加入过购物车
        Cart c=baseMapper.selectByMemberIDAndWaresID(m.getId(),waresId,memberWaresColor,memberWaresVersion);
        if (c == null) {
            //如果没加入过，直接加入
            cart.setAmount(1);
            cart.setWaresId(waresId);
            cart.setMemberId(m.getId());
            cart.setCreateTime(LocalDateTime.now());
            cart.setMemberWaresColor(memberWaresColor);
            cart.setMemberWaresVersion(memberWaresVersion);
            baseMapper.insert(cart);
        }else{
            //加入过
            //判断一下颜色是否相同
            if (c.getMemberWaresColor().equals(memberWaresColor)) {//颜色相同
                //判断版本是否相同
                if (c.getMemberWaresVersion().equals(memberWaresVersion)){//版本相同
                    //把对应商品数量相加
                    baseMapper.updateAmount(c.getId(),waresNum);
                }else{//版本不同
                    //新插入购物车这一条数据
                    cart.setAmount(1);
                    cart.setWaresId(waresId);
                    cart.setMemberId(m.getId());
                    cart.setCreateTime(LocalDateTime.now());
                    cart.setMemberWaresColor(memberWaresColor);
                    cart.setMemberWaresVersion(memberWaresVersion);
                    baseMapper.insert(cart);
                }
            }else{//颜色不同
                //重新插入
                cart.setAmount(1);
                cart.setWaresId(waresId);
                cart.setMemberId(m.getId());
                cart.setCreateTime(LocalDateTime.now());
                cart.setMemberWaresColor(memberWaresColor);
                cart.setMemberWaresVersion(memberWaresVersion);
                baseMapper.insert(cart);
            }
        }
    }

    @Override
    public List<CartWaresVO> selectCurrentMemberWaresInfo(Integer currentMemberId) {
        List<CartWaresVO> list=baseMapper.selectCurrentMemberWaresInfo(currentMemberId);
        return list;
    }

    @Override
    public void doUpdate(Integer currentMemberId, Integer currentWaresId, Integer amount, String currentColor, String currentVersion) {
        baseMapper.doUpdate(currentMemberId,currentWaresId,amount,currentColor,currentVersion);
    }

    @Override
    public Integer selectCartWaresNumberById(Integer id) {
        List<Cart> cartWares=baseMapper.selectCartWaresNumberById(id);
        return cartWares.size();
    }
}
